//
//  UTHomeViewController.swift
//  UniversalTool
//
//  Created by LGB on 2025/3/1.
//

import UIKit

class UTHomeViewController: UTBaseViewController {
    
    @IBOutlet weak var navigationHeight: NSLayoutConstraint!
    
    @IBOutlet weak var translateTextLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        configSubscription()
    }
    
    fileprivate func setupViews() {
        navigationHeight.constant = UIApplication.shared.statusBarFrame.size.height + 44
    }
    
    func configSubscription() {
        YSIAPManager.shared().checkOrderState {} failure: {}
        let status: Bool = UserDefaults.standard.bool(forKey: isFirstOpen)
        if !status {
            UserDefaults.standard.setValue(true, forKey: isFirstOpen)
            goSubscription()
        }
    }
    
    func goSubscription() {
        let vc = UTSubscriptionViewController()
        vc.modalPresentationStyle = .overFullScreen
        self.present(vc, animated: false)
    }
    
    @IBAction func goMine(_ sender: UIButton) {
        let vc = UTMineViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }

    @IBAction func goTranslate(_ sender: UITapGestureRecognizer) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = UTTranslatorViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: utBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = UTTranslatorViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        goSubscription()
        
    }

    @IBAction func goConnector(_ sender: UIButton) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = UTConnectorViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: utBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = UTConnectorViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        goSubscription()
    }
    
    @IBAction func goCounter(_ sender: Any) {
        if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
            let vc = UTCounterViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        if let benefit = UserDefaults.standard.object(forKey: utBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
            let vc = UTCounterViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            return
        }
        goSubscription()
    }
    
}
